package lc.q350_400;

public class Q400 {

    public int findNthDigit(int n) {
        // 个位数1-9
        // 十位数10-99
        // 判断在几位数
        int d = 1;
        long dnn = 0;
        long dn = 9;
        long dStart = 1;
        while (n > dn) {
            dnn = dn;
            d ++;
            dStart = dStart * 10;
            dn = dStart * 9 * d + dn;
        }
        // 判断出当前到了几位数了
        // 减去dnn / d就知道在第几个？
        long num = (n - dnn - 1) / d + dStart;
        // 取num的位数
        int k = (int) ((n - dnn - 1) % d);
        return String.valueOf(num).charAt(k) - '0';
    }

    public static void main(String[] args) {
        Q400 q400 = new Q400();
        System.out.println(q400.findNthDigit(1000000000));
        System.out.println(q400.findNthDigit(200));
        System.out.println(q400.findNthDigit(3));
    }
}
